package com.example.leero.graduation.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.leero.graduation.API;
import com.example.leero.graduation.App;
import com.example.leero.graduation.BaseActivity;
import com.example.leero.graduation.R;
import com.example.leero.graduation.Util.GsonUtil;
import com.example.leero.graduation.Util.ImageLoader;
import com.example.leero.graduation.Util.LogUtil;
import com.example.leero.graduation.Util.StringUtil;
import com.example.leero.graduation.adapter.RankAdapter;
import com.example.leero.graduation.model.Rank;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class RankActivity extends BaseActivity {

    private RecyclerView rv_rank_list;
    private ImageView iv_user_avatar;
    private TextView tv_user_name;
    private TextView tv_mileage;
    private TextView tv_rank_num;

    private RankAdapter adapter;
    private List<Rank> rankList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_rank;
    }

    @Override
    protected void initVariable() {
        iv_user_avatar = (ImageView) findViewById(R.id.iv_user_avatar);
        tv_user_name = (TextView) findViewById(R.id.tv_user_name);
        tv_mileage = (TextView) findViewById(R.id.tv_mileage);
        tv_rank_num = (TextView) findViewById(R.id.tv_rank_num);
    }

    @Override
    protected void initView(Bundle state) {
        setupRv();

        TextView title = (TextView) findViewById(R.id.tv_title);
        title.setText("排行榜");
        findViewById(R.id.iv_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void loadData() {
        OkHttpUtils.post().url(API.GET_RANK)
                .addParams("user_id", App.getLoginUser().id + "")
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (!jsonObject.getString("user_index").equals("0")) {
                        tv_rank_num.setText(jsonObject.getString("user_index"));
                    }
                    rankList.clear();
                    rankList.addAll(GsonUtil.jsonToArrayList(jsonObject.getString("data"), Rank.class));
                    adapter.notifyDataSetChanged();

                    Rank rank = GsonUtil.fromJson(jsonObject.getString("user_data"), Rank.class);
                    handleUserRank(rank);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onAfter(int id) {
                super.onAfter(id);
            }
        });
    }

    private void handleUserRank(Rank rank) {
        if (!StringUtil.isEmpty(rank.avatar)) {
            ImageLoader.load(RankActivity.this, iv_user_avatar, rank.avatar);
        } else {
            iv_user_avatar.setImageResource(R.drawable.default_avatar_cricle);
        }

        tv_user_name.setText(rank.name);
        tv_mileage.setText("跑了" + (rank.mileage / 1000) + "Km");
    }

    private void setupRv() {
        adapter = new RankAdapter(this, R.layout.item_rank, rankList);
        rv_rank_list = (RecyclerView) findViewById(R.id.rv_rank_list);
        rv_rank_list.setLayoutManager(new LinearLayoutManager(this));
        rv_rank_list.setAdapter(adapter);
    }
}
